package com.okilo.online.app.dialog

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.okilo.online.app.OkiloApplication
import com.okilo.online.app.R
import com.okilo.online.app.common.OkiloComDlog
import com.okilo.online.app.databinding.OkiloDlogLimitIncreasedBinding
import com.okilo.online.app.event.OkiloEventPointType
import com.okilo.online.app.tools.OkiloDensityUtil
import org.json.JSONObject
import java.math.BigDecimal


/**
 * OkiloLoan
 * @Author wwdeng
 * @CreateTime: 2024/11/13 15:41
 * @Annotation：产品额度提升dialog
 */
class OkiloHomeLimitIncreasedDlog: OkiloComDlog {
    private lateinit var binding: OkiloDlogLimitIncreasedBinding
    private var mCallback: IDialogCallback? = null

    constructor(context: Context) : super(context) {}
    constructor(context: Context, eventRemark: String, callback: IDialogCallback?) : super(context) {
        dialogRemark = eventRemark
        mCallback = callback
    }

    override val dialogName: String = "Limit_Increased"

    override fun initializeOkiloWindow() {
        val params = window?.attributes
        params?.width = OkiloApplication.app.getPhoneWidth() - OkiloDensityUtil.dp2px(48f)
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.dialog_scale_anim) //修改动画样式
        window?.setGravity(Gravity.CENTER)
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun createOkiloContentView(): View {
        binding = OkiloDlogLimitIncreasedBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initializeOkiloViews() {
        binding.okiloHomeLimitIncreasedButtonApply.text = getString(R.string.okilo_btn_apply).uppercase()
    }

    override fun setupOkiloEventListeners() {
        setOnClickListener(binding.okiloHomeLimitIncreasedButtonApply) //申请
        setOnClickListener(binding.okiloHomeLimitIncreasedButtonClose) //关闭
    }

    override fun loadOkiloData() {
    }

    override fun onOkiloEventViewClick(v: View?) {
        dismiss()
        when(v?.id){
            R.id.okiloHomeLimitIncreasedButtonApply -> {
                addEventPointRequestAction(OkiloEventPointType.OKILO_CLICK_TRACK.LIMIT_INCREASED_APPLY,dialogRemark)
                mCallback?.onClick(v)
            }
            R.id.okiloHomeLimitIncreasedButtonClose -> {
                addEventPointRequestAction(OkiloEventPointType.OKILO_CLICK_TRACK.LIMIT_INCREASED_CLOSE,dialogRemark)
            }
        }
    }

    /**
     * 设置数据
     */
    fun sOkiloHomeLimitIncreasedDlogData(data: JSONObject){
        binding.okiloHomeLimitIncreasedNewAmount.text = BigDecimal(data.optString("amount")).stripTrailingZeros().toPlainString()
        val text = BigDecimal(data.optString("oldAmount")).stripTrailingZeros().toPlainString()
        binding.okiloHomeLimitIncreasedOldAmount.text = text
    }

    interface IDialogCallback {
        //按钮点击
        fun onClick(view: View?)
    }

    /**
     *
     * 接口描述：product额度相较比于上一次是否有变化
     * 接口名：/v1/amountIsUp
     * 参数：公共参数
     * 注意：贷超包product和token参数，传所需要查询的产品对应的值
     * 返回值：
     * openFlag，值"YES"或"NO"
     * 当openFlag=“YES”时，会多两个返回值：amount、oldAmount
     *
     */

}